import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class RunSqlScript {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
        String username = "root";
        String password = "123456";
        String sqlFile = "f:\\大四\\ky\\RuoYi-Vue\\sql\\ry_20250522.sql";

        try {
            // Load driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("Driver loaded successfully");

            // Establish connection
            Connection conn = DriverManager.getConnection(url, username, password);
            System.out.println("Database connected successfully");

            // Read SQL file and execute menu-related SQL
            BufferedReader reader = new BufferedReader(new FileReader(sqlFile));
            String line;
            StringBuilder sqlBuilder = new StringBuilder();
            boolean inMenuSection = false;
            
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                
                // Check if we're in the menu section
                if (line.contains("数据库产品管理")) {
                    inMenuSection = true;
                }
                
                // Execute insert statements
                if (inMenuSection && line.startsWith("insert into sys_menu")) {
                    sqlBuilder.setLength(0);
                    sqlBuilder.append(line);
                    
                    // Handle multi-line statements
                    while (!line.endsWith(";")) {
                        line = reader.readLine().trim();
                        sqlBuilder.append(" ").append(line);
                    }
                    
                    String sql = sqlBuilder.toString();
                    Statement stmt = conn.createStatement();
                    stmt.executeUpdate(sql);
                    System.out.println("Executed SQL: " + sql.substring(0, Math.min(50, sql.length())) + "...");
                }
            }
            
            reader.close();
            conn.close();
            System.out.println("SQL script execution completed");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}